package com.sea.supply.service.impl;


import com.codecrane.core.web.BatchImportUtils;
import com.sea.supply.entity.Supplier;
import com.sea.supply.entity.WareBalance;
import com.sea.supply.entity.WareDistribute;
import com.sea.supply.entity.WareProduce;
import com.sea.supply.mapper.WareBalanceMapper;
import com.sea.supply.mapper.WareDistributeMapper;
import com.sea.supply.service.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class WareBalanceServiceImpl implements WareBalanceService {

    @Autowired
    private WareBalanceMapper wareBalanceMapper;
    @Autowired
    private WareDistributeService wareDistributeService;

    @Override
    public Integer save(WareBalance wareBalance) {
        return wareBalanceMapper.insert(wareBalance);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<WareBalance> list = BatchImportUtils.iteratorForWareBalance(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU收货信息！");
            } else {

                for (WareBalance receive: list) {
                    WareDistribute wareDistribute=new WareDistribute();
                    String produceNo = receive.getProduceNo();
                    String receiveChannelName = receive.getReceiveChannelName();
                    String receiveSku = receive.getReceiveSku();
                    Integer receiveNumber = receive.getReceiveNumber();
                    String receiveRemark = receive.getReceiveRemark();

                    wareDistribute.setProduceNo(produceNo);
                    wareDistribute.setReceiveChannelName(receiveChannelName);
                    wareDistribute.setReceiveSku(receiveSku);
                    wareDistribute.setWareName(receive.getWareName());

                    WareDistribute distribute = wareDistributeService.findBySkuChannelNo(wareDistribute);

                    if(null==distribute && !wareDistribute.getReceiveChannelName().contains("Artecho")){
                        throw new RuntimeException("暂无查询到数据！【SKU:" + receiveSku + ";渠道:" + receiveChannelName + ";PO:" + produceNo + ";仓库:" + receive.getWareName() + "】");
                    }else if (null==distribute && wareDistribute.getReceiveChannelName().contains("Artecho")){
                        WareDistribute w=new WareDistribute();
                        BeanUtils.copyProperties(wareDistribute,w);
                        w.setReceiveNumber(0);
                        w.setProduceUse(0);
                        wareDistributeService.save(w);
                        distribute=w;
                    }

                    //盘盈
                    Integer number = distribute.getReceiveNumber();
                    if(receiveNumber>=0){
                        distribute.setReceiveNumber(number+receiveNumber);
                        wareDistributeService.usave(distribute);
                    }

                    if(receiveNumber<0){
                        Integer num = distribute.getReceiveNumber()-distribute.getProduceUse()+receiveNumber;
                        if(num<0){
                            throw new RuntimeException("盘亏数据不能为负数！【SKU:" + receiveSku + ";渠道:" + receiveChannelName + ";PO:" + produceNo + ";仓库:" + receive.getWareName() + "】");
                        }
                        distribute.setReceiveNumber(number+receiveNumber);
                        wareDistributeService.usave(distribute);
                    }

                    receive.setProduceDetailId(distribute.getId());
                    receive.setReceiveNumber(receiveNumber);
                    receive.setReceiveRemark(receiveRemark);
                    receive.setReceiveMember(userRealName);
                    wareBalanceMapper.insert(receive);

                }
            }
        }
    }

    @Override
    public List<WareBalance> findByCnd(WareBalance wareBalance) {
        return wareBalanceMapper.findByCnd(wareBalance);
    }
}
